package csc.traning.wpsj.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;

import javax.security.auth.callback.ConfirmationCallback;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.org.apache.xml.internal.security.Init;

import csc.traning.wpsj.dao.Counter;
import csc.traning.wpsj.dao.EmployeesDB;
import csc.traning.wpsj.dao.User;
import csc.traning.wpsj.dao.UserDB;
import csc.traning.wpsj.servlet.lessons.CookieUtilities;

/**
 * Servlet implementation class Logon
 * 
 * @author hdang20
 */
public class EditAdminPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditAdminPassword() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<String> error = new ArrayList<String>();
		String nav = null;
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		String newPassword = request.getParameter("newPassword");
		String confirmPassword = request.getParameter("confirmPassword");
		if (!"".equals(confirmPassword) || !"".equals(newPassword))
			if (!newPassword.equals(confirmPassword))
				error.add("New password and confirm new password is not the same");

		if (error.size() == 0) {
			nav = "view-employees";
			if (!"".equals(newPassword))
				UserDB.changePassword(u, newPassword);
		} else {
			request.setAttribute("listError", error);
			nav = "editPasswordAdmin.jsp";
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher(nav);
		dispatcher.forward(request, response);
	}

}
